﻿#ifndef PST_SURFACEELECTRICFIELDMAPWINDOW_H
#define PST_SURFACEELECTRICFIELDMAPWINDOW_H

#include "Graph3DWindowBase.h"
#include <vtkSmartPointer.h>
#include <QHash>
#include <QList>
#include "SurfaceElectricFieldMapSettingData.h"
#include "InfomationGraph3DBasical.h"

class vtkProp;
class QStandardItem;
class QListWidgetItem;
class vtkScalarBarActor;

namespace pst
{
    class PropPickerCallbackHander;
    class SurfaceElectricFieldMapSettingDialog;
    class SurfaceElectricFieldMapSidebarSettings;
    class SurfaceElectricFieldMapSidebarData;

    class SurfaceElectricFieldMapWindow : public Graph3DWindowBase
    {
        Q_OBJECT
    public:
        SurfaceElectricFieldMapWindow(QWidget* parent, int windowId);

        void setWindowID(int windowID) override;
        int getWindowID() const override;

        void setWindowUniversalID(int windowUniversalID) override;
        int getWindowUniversalID() const override;

        /// @brief 文件写出至工程文件
        QDomElement& writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso = false) override;

        /// @brief 从工程文件读入数据
        void readFromProjectFile(QDomElement* element, bool isdiso = false) override;

        void addGraph3D(InfomationGraph3DBasical* info) override;

        QList<InfomationGraph3DBasical*> getGraph3DInfoList();

        void setActiveAnimation(bool active) override;

        void exportAnimationData(const QString& path) override;

        void exportSourceDataToLocal() override;

        void pyExportSourceDataToLocal(QString filePath) override;

        void addAMarkerDataToPickedDataDisplayWindow(Marker3DDataInfo* info) override;

        void updateClipper(bool activeClipper) override;

        void updateClipperPlaneActor();

        // 根据数据判断是否可以启用动图
        bool whetherCanEnableDataAnimation() override;

        // 根据数据判断是否可以启用雷电分区
        bool whetherCanEnableThunderstormZone();

        //获取图中表面电势分布对应的item指针，如果不存在该类型返回nullptr
        //表面场图不允许添加多个数据，因此返回的item指针只有一个
        QStandardItem* getSurfacePotentialDistributionSourceItem();


    private:
        void initScalarBarSettingConnection();
        void initSidebarSettingConnection();
        void generateSidebarSettingData(QListWidgetItem* item, InfomationGraph3DBasical* info);

        void connectSignalsAndSlots();
        void disconnectSignalsAndSlots();

        void prepareAnimationData(); // 准备动画数据
        void removeActorWithMarker(InfomationGraph3DBasical* info);

        void updateMarkerValue() override; // 更新marker的值，比如重新选择了场值，动图更新数据
        void animationToNextDataFrame() override;

        //单个info是否包含多个数据，是否可以用于动图
        bool isInfoContainsMultipleData(const InfomationGraph3DBasical* info) override;
        //判断多个info是否是同一来源，是否可以用于动图
        bool isInfoFromSameSource(QList< InfomationGraph3DBasical*> infoList) override;

        void sentToolBarAnimationDataInfo(const InfomationGraph3DBasical* info) override;

        QString generateGraphNameAuto(InfomationGraph3DBasical* info) const;

        SurfaceElectricFieldMapSettingData* getMainSettingData(InfomationGraph3DBasical* info);

        // 更新db 和 unit
        void updateDBAndUnitData() override;

        bool getHasSameName(const QString& name) const;

        SurfaceElectricFieldMapSidebarData* getSidebarDataFromNameItem(QListWidgetItem* item);
        InfomationGraph3DBasical* getGraph3DInfoFromSidebarData(SurfaceElectricFieldMapSidebarData* data);

        void updateGeometryActors(bool isShow, bool& hasAnyGeometrySuccess);

        void updateMarkerVisibility() override;

        void slot_addCustomContextMenu(const QPoint& pos) override;

    public slots:
        void slot_scalarBarDoubleClicked(vtkProp* pickedProp) override;
        void slot_scalarBarFontTypeChanged(vtkScalarBarActor* actor, QString fontType);
        void slot_updateSetting(SurfaceElectricFieldMapSettingData* data);
        void slot_showHideItemActor(QListWidgetItem* item);
        void slot_graphNameDoubleClicked(QListWidgetItem* item);
        void slot_updateActorValue(SurfaceElectricFieldMapSidebarData* data);
        void slot_chosenItemsChanged(const QList<QListWidgetItem*>& itemList);
        void slot_deleteChosenGraphs(const QList<QListWidgetItem*>& itemList);
        void slot_getSidebarCurveDataSource(QListWidgetItem* item, QString& dataSource);
        void slot_renameCheck();
        bool slot_checkResultNameIsValid(SurfaceElectricFieldMapSettingData* data, const QString& newName);
        void synchronousActorName(InfomationGraph3DBasical* info, const QString& newName);

        //左侧关闭工程树，删除相应的图像
        // 注意items是左侧工程树的节点
        void slot_closeTree(const QList<QStandardItem*>& items);

    public: // python调用
        void pyCurveNameClicked(QList<int> selectedIndexs);
        void pyResultDataChanged(int index);
        void pyUpdateProcessData(bool isLog, bool isNormalize);
        void pyNameItemRename(int itemIndex, QString newName);
        void pyDeleteSurfaceElectricFieldMap(QString selectedIndexs);
        void pyItemCheckStateChanged(int itemIndex, bool visible);
        void pySaveSurfaceElectricFieldMapMainSettingData(int type, int settingDataId, bool scalarBarIsAutoRange,
            double scalarBarMin, double scalarBarMax, bool scalarBarIsShowTitle,
            QString scalarBarTitle, QString scalarBarFontColor, int scalarBarFontSize,
            int geometryTransparency, QString resultGraphName, int resultGraphModelTransparency, bool cubeAxesIsVisible);

    private:
        int m_windowUniversalID{-1};
        int m_windowId;
        SurfaceElectricFieldMapSettingDialog* m_mainSettingDialog;
        SurfaceElectricFieldMapSidebarSettings* m_sidebarSettings;

        QVector<FieldGraphGenerator::ArrayInformation> m_allScalarFieldArray;
        QVector<FieldGraphGenerator::ArrayInformation> m_allVectorFieldArray;

        //QList<QPair<InfomationGraph3DBasical*, SurfaceElectricFieldMapSettingData*>> m_dataMap;
        vtkSmartPointer<PropPickerCallbackHander> m_doubleClickCallback;

        //GeometrySettingData* m_geometrySettingData;
        CubeAxesActorSettingData* m_cubeAxesSettingData;
        //SurfaceElectricFieldMapSettingData* m_commonSetting; // 总体设定

        //QHash<QListWidgetItem*, SurfaceElectricFieldMapSidebarData*> m_sideBarSettingData;

        bool m_isAnimationDataPrepared;
        int m_scalarBarEventId;
    };
} // namespace pst

#endif
